home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9084 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.5 KB  |  71 lines

  1. Newsgroups: comp.lang.c
  2. Path: cwi.nl!dik
  3. From: dik@cwi.nl (Dik T. Winter)
  4. Subject: Re: Tradition or what?
  5. Message-ID: <DnxB7J.G7r@cwi.nl>
  6. Sender: news@cwi.nl (The Daily Dross)
  7. Nntp-Posting-Host: chrysant.cwi.nl
  8. Organization: CWI, Amsterdam
  9. References: <danpop.825961872@rscernix> <313C5CD1.1209@oc.com> <danpop.826211387@rscernix>
  10. Date: Fri, 8 Mar 1996 00:40:30 GMT
  11.  
  12. In article <danpop.826211387@rscernix> danpop@mail.cern.ch (Dan Pop) writes:
  13. ...
  14.  > >> >Here are the three worst Microsoft coding rules, in my opinion at least:
  15. ...
  16.  > >> >Any number greater than 2 must be a named constant.
  17. ...
  18.  > >> Actually, the second rule is quite good (except that '2' should be replaced
  19.  > >> by '1' :-)  Magic constants embedded in the code are a huge nuisance
  20.  > >> for maintenability.  If you have to change an explicit (aka magic) constant
  21.  > >> in a piece of code, how can you be sure that you aren't going to break
  22.  > >> anything?  By changing a macro definition, you _know_ that you won't
  23.  > >> break anything, because all the dependencies of that macro will be
  24.  > >> automatically taken care of by the compiler (and your "make" utility). 
  25.  
  26. This actually depends on the use of the constant.  Changing a constant can
  27. actually break code whether it is done in a manifest constant or in a
  28. named constant.
  29.  > >
  30.  > >I agree that there are good uses for giving a literal a name, and using
  31.  > >that name instead of repeated usage of the literal, but to make a
  32.  > >mandate that even if the literal were referenced only once you must give
  33.  > >it a name would seem too extreme.
  34.  > 
  35.  > The point is that software is not usually cast in concrete.  What you
  36.  > thought to be a one-time magic constant might be used later (in a
  37.  > future release/version of the program) in another place and, if you
  38.  > don't follow the rule, chances are that it will be used again
  39.  
  40. If I write in some numeric code (using floats):
  41.     approx = (f(x - h) + f(x) * 2.0 + f(x + h)) / (2.0 * h);
  42. I am darn sure that all other occurences of the constants 2.0
  43. are only accidental and unrelated to these occurences.  Moreover, I do
  44. know that if I change one or both of the constants the code might fail
  45. in a horrible fashion.  Or do you think the following is more readable:
  46.     approx = (f(x = h) + f(x) * TWO + f(x + h)) / (TWO * h);
  47. or do you have other suggestions for silly names here? Moreover, in
  48. Gaussian quadrature formulas it is in many cases better to just write-out
  49. the constants than replace them by meaningless macronames like
  50. COEFFICIENT_0 etc. (and yes, I have no idea about finding more meaningful
  51. names).  Such formula's are cast in concrete; you can not just replace
  52. one of the constants, you can only replace the complete formula.  And
  53. numerical mathematics has an awful lot of similar constants (and PI is
  54. not one of them; that is an obvious candidate for a macro).
  55.  
  56. Similar things do happen in integer code too.  Take binary search; you
  57. happen to divide by two a lot.  Change that to a macro?  What sensible
  58. name do you suggest; and what would be the result if you change the
  59. constant?  Moreover, the suggestion that using macro's makes changes
  60. easier and simpler to comprehend are *not* true.  They lead to the
  61. cases where code starts with:
  62.   #define THREE 4
  63. and in many cases it is not possible (in my opinion) to pick up more
  64. sensible names.
  65.  
  66. The rule as such is *not* good.  There are too many cases where the
  67. rule obscures the actual algorithm used.
  68. -- 
  69. dik t. winter, cwi, kruislaan 413, 1098 sj  amsterdam, nederland, +31205924098
  70. home: bovenover 215, 1025 jn  amsterdam, nederland; http://www.cwi.nl/~dik/
  71.